'\" t
.\"     Title: zmq_socket
.\"    Author: [see the "AUTHORS" section]
.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
.\"      Date: 07/08/2019
.\"    Manual: 0MQ Manual
.\"    Source: 0MQ 4.3.2
.\"  Language: English
.\"
.TH "ZMQ_SOCKET" "3" "07/08/2019" "0MQ 4\&.3\&.2" "0MQ Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
zmq_socket \- create 0MQ socket
.SH "SYNOPSIS"
.sp
\fBvoid *zmq_socket (void \fR\fB\fI*context\fR\fR\fB, int \fR\fB\fItype\fR\fR\fB);\fR
.SH "DESCRIPTION"
.sp
The \fIzmq_socket()\fR function shall create a 0MQ socket within the specified \fIcontext\fR and return an opaque handle to the newly created socket\&. The \fItype\fR argument specifies the socket type, which determines the semantics of communication over the socket\&.
.sp
The newly created socket is initially unbound, and not associated with any endpoints\&. In order to establish a message flow a socket must first be connected to at least one endpoint with \fBzmq_connect\fR(3), or at least one endpoint must be created for accepting incoming connections with \fBzmq_bind\fR(3)\&.
.PP
\fBKey differences to conventional sockets\fR. Generally speaking, conventional sockets present a
\fIsynchronous\fR
interface to either connection\-oriented reliable byte streams (SOCK_STREAM), or connection\-less unreliable datagrams (SOCK_DGRAM)\&. In comparison, 0MQ sockets present an abstraction of an asynchronous
\fImessage queue\fR, with the exact queueing semantics depending on the socket type in use\&. Where conventional sockets transfer streams of bytes or discrete datagrams, 0MQ sockets transfer discrete
\fImessages\fR\&.
.sp
0MQ sockets being \fIasynchronous\fR means that the timings of the physical connection setup and tear down, reconnect and effective delivery are transparent to the user and organized by 0MQ itself\&. Further, messages may be \fIqueued\fR in the event that a peer is unavailable to receive them\&.
.sp
Conventional sockets allow only strict one\-to\-one (two peers), many\-to\-one (many clients, one server), or in some cases one\-to\-many (multicast) relationships\&. With the exception of \fIZMQ_PAIR\fR, 0MQ sockets may be connected \fBto multiple endpoints\fR using \fIzmq_connect()\fR, while simultaneously accepting incoming connections \fBfrom multiple endpoints\fR bound to the socket using \fIzmq_bind()\fR, thus allowing many\-to\-many relationships\&.
.PP
\fBThread safety\fR. 0MQ has both thread safe socket type and
\fInot\fR
thread safe socket types\&. Applications MUST NOT use a
\fInot\fR
thread safe socket from multiple threads except after migrating a socket from one thread to another with a "full fence" memory barrier\&.
.sp
Following are the thread safe sockets: * ZMQ_CLIENT * ZMQ_SERVER * ZMQ_DISH * ZMQ_RADIO * ZMQ_SCATTER * ZMQ_GATHER
.PP
\fBSocket types\fR. The following sections present the socket types defined by 0MQ, grouped by the general
\fImessaging pattern\fR
which is built from related socket types\&.
.SS "Client\-server pattern"
.sp
The client\-server pattern is used to allow a single \fIZMQ_SERVER\fR \fIserver\fR talk to one or more \fIZMQ_CLIENT\fR \fIclients\fR\&. The client always starts the conversation, after which either peer can send messages asynchronously, to the other\&.
.sp
The client\-server pattern is formally defined by \m[blue]\fBhttp://rfc\&.zeromq\&.org/spec:41\fR\m[]\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Server\-client is still in draft phase\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_CLIENT\fR
.RS 4
.sp
A \fIZMQ_CLIENT\fR socket talks to a \fIZMQ_SERVER\fR socket\&. Either peer can connect, though the usual and recommended model is to bind the \fIZMQ_SERVER\fR and connect the \fIZMQ_CLIENT\fR\&.
.sp
If the \fIZMQ_CLIENT\fR socket has established a connection, \fBzmq_send\fR(3) will accept messages, queue them, and send them as rapidly as the network allows\&. The outgoing buffer limit is defined by the high water mark for the socket\&. If the outgoing buffer is full, or if there is no connected peer, \fBzmq_send\fR(3) will block, by default\&. The \fIZMQ_CLIENT\fR socket will not drop messages\&.
.sp
When a \fIZMQ_CLIENT\fR socket is connected to multiple \fIZMQ_SERVER\fR sockets, outgoing messages are distributed between connected peers on a round\-robin basis\&. Likewise, the \fIZMQ_CLIENT\fR socket receives messages fairly from each connected peer\&. This usage is sensible only for stateless protocols\&.
.sp
\fIZMQ_CLIENT\fR sockets are threadsafe and can be used from multiple threads at the same time\&. Note that replies from a \fIZMQ_SERVER\fR socket will go to the first client thread that calls \fBzmq_msg_recv\fR(3)\&. If you need to get replies back to the originating thread, use one \fIZMQ_CLIENT\fR socket per thread\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIZMQ_CLIENT\fR sockets are threadsafe\&. They do not accept the ZMQ_SNDMORE option on sends not ZMQ_RCVMORE on receives\&. This limits them to single part data\&. The intention is to extend the API to allow scatter/gather of multi\-part data\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&1.\ \&Summary of ZMQ_CLIENT characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_SERVER\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Round\-robin
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_SERVER\fR
.RS 4
.sp
A \fIZMQ_SERVER\fR socket talks to a set of \fIZMQ_CLIENT\fR sockets\&. A \fIZMQ_SERVER\fR socket can only reply to an incoming message: the \fIZMQ_CLIENT\fR peer must always initiate a conversation\&.
.sp
Each received message has a \fIrouting_id\fR that is a 32\-bit unsigned integer\&. The application can fetch this with \fBzmq_msg_routing_id\fR(3)\&. To send a message to a given \fIZMQ_CLIENT\fR peer the application must set the peer\(cqs \fIrouting_id\fR on the message, using \fBzmq_msg_set_routing_id\fR(3)\&.
.sp
If the \fIrouting_id\fR is not specified, or does not refer to a connected client peer, the send call will fail with EHOSTUNREACH\&. If the outgoing buffer for the client peer is full, the send call shall block, unless ZMQ_DONT_WAIT is used in the send, in which case it shall fail with EAGAIN\&. The \fIZMQ_SERVER\fR socket shall not drop messages in any case\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIZMQ_SERVER\fR sockets are threadsafe\&. They do not accept the ZMQ_SNDMORE option on sends not ZMQ_RCVMORE on receives\&. This limits them to single part data\&. The intention is to extend the API to allow scatter/gather of multi\-part data\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&2.\ \&Summary of ZMQ_SERVER characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_CLIENT\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
See text
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Action in mute state
T}:T{
.sp
Return EAGAIN
T}
.TE
.sp 1
.RE
.SS "Radio\-dish pattern"
.sp
The radio\-dish pattern is used for one\-to\-many distribution of data from a single \fIpublisher\fR to multiple \fIsubscribers\fR in a fan out fashion\&.
.sp
Radio\-dish is using groups (vs Pub\-sub topics), Dish sockets can join a group and each message sent by Radio sockets belong to a group\&.
.sp
Groups are null terminated strings limited to 16 chars length (including null)\&. The intention is to increase the length to 40 chars (including null)\&. The encoding of groups shall be UTF8\&.
.sp
Groups are matched using exact matching (vs prefix matching of PubSub)\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Radio\-dish is still in draft phase\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_RADIO\fR
.RS 4
.sp
A socket of type \fIZMQ_RADIO\fR is used by a \fIpublisher\fR to distribute data\&. Each message belong to a group, a group is specified with \fBzmq_msg_set_group\fR(3)\&. Messages are distributed to all members of a group\&. The \fBzmq_recv\fR(3) function is not implemented for this socket type\&.
.sp
When a \fIZMQ_RADIO\fR socket enters the \fImute\fR state due to having reached the high water mark for a \fIsubscriber\fR, then any messages that would be sent to the \fIsubscriber\fR in question shall instead be dropped until the mute state ends\&. The \fIzmq_send()\fR function shall never block for this socket type\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIZMQ_RADIO\fR sockets are threadsafe\&. They do not accept the ZMQ_SNDMORE option on sends\&. This limits them to single part data\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&3.\ \&Summary of ZMQ_RADIO characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_DISH\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Send only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Fan out
T}
T{
.sp
Action in mute state
T}:T{
.sp
Drop
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_DISH\fR
.RS 4
.sp
A socket of type \fIZMQ_DISH\fR is used by a \fIsubscriber\fR to subscribe to groups distributed by a \fIradio\fR\&. Initially a \fIZMQ_DISH\fR socket is not subscribed to any groups, use \fBzmq_join\fR(3) to join a group\&. To get the group the message belong to call \fBzmq_msg_group\fR(3)\&. The \fIzmq_send()\fR function is not implemented for this socket type\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIZMQ_DISH\fR sockets are threadsafe\&. They do not accept ZMQ_RCVMORE on receives\&. This limits them to single part data\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&4.\ \&Summary of ZMQ_DISH characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_RADIO\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Receive only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
N/A
T}
.TE
.sp 1
.RE
.SS "Publish\-subscribe pattern"
.sp
The publish\-subscribe pattern is used for one\-to\-many distribution of data from a single \fIpublisher\fR to multiple \fIsubscribers\fR in a fan out fashion\&.
.sp
The publish\-subscribe pattern is formally defined by \m[blue]\fBhttp://rfc\&.zeromq\&.org/spec:29\fR\m[]\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_PUB\fR
.RS 4
.sp
A socket of type \fIZMQ_PUB\fR is used by a \fIpublisher\fR to distribute data\&. Messages sent are distributed in a fan out fashion to all connected peers\&. The \fBzmq_recv\fR(3) function is not implemented for this socket type\&.
.sp
When a \fIZMQ_PUB\fR socket enters the \fImute\fR state due to having reached the high water mark for a \fIsubscriber\fR, then any messages that would be sent to the \fIsubscriber\fR in question shall instead be dropped until the mute state ends\&. The \fIzmq_send()\fR function shall never block for this socket type\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&5.\ \&Summary of ZMQ_PUB characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_SUB\fR, \fIZMQ_XSUB\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Send only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Fan out
T}
T{
.sp
Action in mute state
T}:T{
.sp
Drop
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_SUB\fR
.RS 4
.sp
A socket of type \fIZMQ_SUB\fR is used by a \fIsubscriber\fR to subscribe to data distributed by a \fIpublisher\fR\&. Initially a \fIZMQ_SUB\fR socket is not subscribed to any messages, use the \fIZMQ_SUBSCRIBE\fR option of \fBzmq_setsockopt\fR(3) to specify which messages to subscribe to\&. The \fIzmq_send()\fR function is not implemented for this socket type\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&6.\ \&Summary of ZMQ_SUB characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_PUB\fR, \fIZMQ_XPUB\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Receive only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
N/A
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_XPUB\fR
.RS 4
.sp
Same as ZMQ_PUB except that you can receive subscriptions from the peers in form of incoming messages\&. Subscription message is a byte 1 (for subscriptions) or byte 0 (for unsubscriptions) followed by the subscription body\&. Messages without a sub/unsub prefix are also received, but have no effect on subscription status\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&7.\ \&Summary of ZMQ_XPUB characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_SUB\fR, \fIZMQ_XSUB\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Send messages, receive subscriptions
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Fan out
T}
T{
.sp
Action in mute state
T}:T{
.sp
Drop
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_XSUB\fR
.RS 4
.sp
Same as ZMQ_SUB except that you subscribe by sending subscription messages to the socket\&. Subscription message is a byte 1 (for subscriptions) or byte 0 (for unsubscriptions) followed by the subscription body\&. Messages without a sub/unsub prefix may also be sent, but have no effect on subscription status\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&8.\ \&Summary of ZMQ_XSUB characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_PUB\fR, \fIZMQ_XPUB\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Receive messages, send subscriptions
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Action in mute state
T}:T{
.sp
Drop
T}
.TE
.sp 1
.RE
.SS "Pipeline pattern"
.sp
The pipeline pattern is used for distributing data to \fInodes\fR arranged in a pipeline\&. Data always flows down the pipeline, and each stage of the pipeline is connected to at least one \fInode\fR\&. When a pipeline stage is connected to multiple \fInodes\fR data is round\-robined among all connected \fInodes\fR\&.
.sp
The pipeline pattern is formally defined by \m[blue]\fBhttp://rfc\&.zeromq\&.org/spec:30\fR\m[]\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_PUSH\fR
.RS 4
.sp
A socket of type \fIZMQ_PUSH\fR is used by a pipeline \fInode\fR to send messages to downstream pipeline \fInodes\fR\&. Messages are round\-robined to all connected downstream \fInodes\fR\&. The \fIzmq_recv()\fR function is not implemented for this socket type\&.
.sp
When a \fIZMQ_PUSH\fR socket enters the \fImute\fR state due to having reached the high water mark for all downstream \fInodes\fR, or if there are no downstream \fInodes\fR at all, then any \fBzmq_send\fR(3) operations on the socket shall block until the mute state ends or at least one downstream \fInode\fR becomes available for sending; messages are not discarded\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&9.\ \&Summary of ZMQ_PUSH characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_PULL\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Send only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Round\-robin
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_PULL\fR
.RS 4
.sp
A socket of type \fIZMQ_PULL\fR is used by a pipeline \fInode\fR to receive messages from upstream pipeline \fInodes\fR\&. Messages are fair\-queued from among all connected upstream \fInodes\fR\&. The \fIzmq_send()\fR function is not implemented for this socket type\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&10.\ \&Summary of ZMQ_PULL characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_PUSH\fR
T}
T{
.sp
Direction
T}:T{
.sp
Unidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Receive only
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.SS "Exclusive pair pattern"
.sp
The exclusive pair pattern is used to connect a peer to precisely one other peer\&. This pattern is used for inter\-thread communication across the inproc transport\&.
.sp
The exclusive pair pattern is formally defined by \m[blue]\fBhttp://rfc\&.zeromq\&.org/spec:31\fR\m[]\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_PAIR\fR
.RS 4
.sp
A socket of type \fIZMQ_PAIR\fR can only be connected to a single peer at any one time\&. No message routing or filtering is performed on messages sent over a \fIZMQ_PAIR\fR socket\&.
.sp
When a \fIZMQ_PAIR\fR socket enters the \fImute\fR state due to having reached the high water mark for the connected peer, or if no peer is connected, then any \fBzmq_send\fR(3) operations on the socket shall block until the peer becomes available for sending; messages are not discarded\&.
.sp
While \fIZMQ_PAIR\fR sockets can be used over transports other than \fBzmq_inproc\fR(7), their inability to auto\-reconnect coupled with the fact new incoming connections will be terminated while any previous connections (including ones in a closing state) exist makes them unsuitable for TCP in most cases\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIZMQ_PAIR\fR sockets are designed for inter\-thread communication across the \fBzmq_inproc\fR(7) transport and do not implement functionality such as auto\-reconnection\&.
.sp .5v
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&11.\ \&Summary of ZMQ_PAIR characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_PAIR\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
N/A
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.SS "Native Pattern"
.sp
The native pattern is used for communicating with TCP peers and allows asynchronous requests and replies in either direction\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_STREAM\fR
.RS 4
.sp
A socket of type \fIZMQ_STREAM\fR is used to send and receive TCP data from a non\-0MQ peer, when using the tcp:// transport\&. A \fIZMQ_STREAM\fR socket can act as client and/or server, sending and/or receiving TCP data asynchronously\&.
.sp
When receiving TCP data, a \fIZMQ_STREAM\fR socket shall prepend a message part containing the \fIrouting id\fR of the originating peer to the message before passing it to the application\&. Messages received are fair\-queued from among all connected peers\&.
.sp
When sending TCP data, a \fIZMQ_STREAM\fR socket shall remove the first part of the message and use it to determine the \fIrouting id\fR of the peer the message shall be routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error\&.
.sp
To open a connection to a server, use the zmq_connect call, and then fetch the socket routing id using the zmq_getsockopt call with the ZMQ_ROUTING_ID option\&.
.sp
To close a specific connection, send the routing id frame followed by a zero\-length message (see EXAMPLE section)\&.
.sp
When a connection is made, a zero\-length message will be received by the application\&. Similarly, when the peer disconnects (or the connection is lost), a zero\-length message will be received by the application\&.
.sp
You must send one routing id frame followed by one data frame\&. The ZMQ_SNDMORE flag is required for routing id frames but is ignored on data frames\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&12.\ \&Summary of ZMQ_STREAM characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
none\&.
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
See text
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Action in mute state
T}:T{
.sp
EAGAIN
T}
.TE
.sp 1
.RE
.SS "Request\-reply pattern"
.sp
The request\-reply pattern is used for sending requests from a ZMQ_REQ \fIclient\fR to one or more ZMQ_REP \fIservices\fR, and receiving subsequent replies to each request sent\&.
.sp
The request\-reply pattern is formally defined by \m[blue]\fBhttp://rfc\&.zeromq\&.org/spec:28\fR\m[]\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_REQ\fR
.RS 4
.sp
A socket of type \fIZMQ_REQ\fR is used by a \fIclient\fR to send requests to and receive replies from a \fIservice\fR\&. This socket type allows only an alternating sequence of \fIzmq_send(request)\fR and subsequent \fIzmq_recv(reply)\fR calls\&. Each request sent is round\-robined among all \fIservices\fR, and each reply received is matched with the last issued request\&.
.sp
If no services are available, then any send operation on the socket shall block until at least one \fIservice\fR becomes available\&. The REQ socket shall not discard messages\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&13.\ \&Summary of ZMQ_REQ characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_REP\fR, \fIZMQ_ROUTER\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Send, Receive, Send, Receive, \&...
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Round\-robin
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Last peer
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_REP\fR
.RS 4
.sp
A socket of type \fIZMQ_REP\fR is used by a \fIservice\fR to receive requests from and send replies to a \fIclient\fR\&. This socket type allows only an alternating sequence of \fIzmq_recv(request)\fR and subsequent \fIzmq_send(reply)\fR calls\&. Each request received is fair\-queued from among all \fIclients\fR, and each reply sent is routed to the \fIclient\fR that issued the last request\&. If the original requester does not exist any more the reply is silently discarded\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&14.\ \&Summary of ZMQ_REP characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_REQ\fR, \fIZMQ_DEALER\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Receive, Send, Receive, Send, \&...
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Last peer
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_DEALER\fR
.RS 4
.sp
A socket of type \fIZMQ_DEALER\fR is an advanced pattern used for extending request/reply sockets\&. Each message sent is round\-robined among all connected peers, and each message received is fair\-queued from all connected peers\&.
.sp
When a \fIZMQ_DEALER\fR socket enters the \fImute\fR state due to having reached the high water mark for all peers, or if there are no peers at all, then any \fBzmq_send\fR(3) operations on the socket shall block until the mute state ends or at least one peer becomes available for sending; messages are not discarded\&.
.sp
When a \fIZMQ_DEALER\fR socket is connected to a \fIZMQ_REP\fR socket each message sent must consist of an empty message part, the \fIdelimiter\fR, followed by one or more \fIbody parts\fR\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&15.\ \&Summary of ZMQ_DEALER characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_ROUTER\fR, \fIZMQ_REP\fR, \fIZMQ_DEALER\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
Round\-robin
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Action in mute state
T}:T{
.sp
Block
T}
.TE
.sp 1
.RE
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBZMQ_ROUTER\fR
.RS 4
.sp
A socket of type \fIZMQ_ROUTER\fR is an advanced socket type used for extending request/reply sockets\&. When receiving messages a \fIZMQ_ROUTER\fR socket shall prepend a message part containing the \fIrouting id\fR of the originating peer to the message before passing it to the application\&. Messages received are fair\-queued from among all connected peers\&. When sending messages a \fIZMQ_ROUTER\fR socket shall remove the first part of the message and use it to determine the _routing id _ of the peer the message shall be routed to\&. If the peer does not exist anymore, or has never existed, the message shall be silently discarded\&. However, if \fIZMQ_ROUTER_MANDATORY\fR socket option is set to \fI1\fR, the socket shall fail with EHOSTUNREACH in both cases\&.
.sp
When a \fIZMQ_ROUTER\fR socket enters the \fImute\fR state due to having reached the high water mark for all peers, then any messages sent to the socket shall be dropped until the mute state ends\&. Likewise, any messages routed to a peer for which the individual high water mark has been reached shall also be dropped\&. If, \fIZMQ_ROUTER_MANDATORY\fR is set to \fI1\fR, the socket shall block or return EAGAIN in both cases\&.
.sp
When a \fIZMQ_ROUTER\fR socket has \fIZMQ_ROUTER_MANDATORY\fR flag set to \fI1\fR, the socket shall generate \fIZMQ_POLLIN\fR events upon reception of messages from one or more peers\&. Likewise, the socket shall generate \fIZMQ_POLLOUT\fR events when at least one message can be sent to one or more peers\&.
.sp
When a \fIZMQ_REQ\fR socket is connected to a \fIZMQ_ROUTER\fR socket, in addition to the \fIrouting id\fR of the originating peer each message received shall contain an empty \fIdelimiter\fR message part\&. Hence, the entire structure of each received message as seen by the application becomes: one or more \fIrouting id\fR parts, \fIdelimiter\fR part, one or more \fIbody parts\fR\&. When sending replies to a \fIZMQ_REQ\fR socket the application must include the \fIdelimiter\fR part\&.
.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table\ \&16.\ \&Summary of ZMQ_ROUTER characteristics
.TS
tab(:);
lt lt
lt lt
lt lt
lt lt
lt lt
lt lt.
T{
.sp
Compatible peer sockets
T}:T{
.sp
\fIZMQ_DEALER\fR, \fIZMQ_REQ\fR, \fIZMQ_ROUTER\fR
T}
T{
.sp
Direction
T}:T{
.sp
Bidirectional
T}
T{
.sp
Send/receive pattern
T}:T{
.sp
Unrestricted
T}
T{
.sp
Outgoing routing strategy
T}:T{
.sp
See text
T}
T{
.sp
Incoming routing strategy
T}:T{
.sp
Fair\-queued
T}
T{
.sp
Action in mute state
T}:T{
.sp
Drop (see text)
T}
.TE
.sp 1
.RE
.SH "RETURN VALUE"
.sp
The \fIzmq_socket()\fR function shall return an opaque handle to the newly created socket if successful\&. Otherwise, it shall return NULL and set \fIerrno\fR to one of the values defined below\&.
.SH "ERRORS"
.PP
\fBEINVAL\fR
.RS 4
The requested socket
\fItype\fR
is invalid\&.
.RE
.PP
\fBEFAULT\fR
.RS 4
The provided
\fIcontext\fR
is invalid\&.
.RE
.PP
\fBEMFILE\fR
.RS 4
The limit on the total number of open 0MQ sockets has been reached\&.
.RE
.PP
\fBETERM\fR
.RS 4
The context specified was terminated\&.
.RE
.SH "EXAMPLE"
.PP
\fBCreating a simple HTTP server using ZMQ_STREAM\fR. 
.sp
.if n \{\
.RS 4
.\}
.nf
void *ctx = zmq_ctx_new ();
assert (ctx);
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM);
assert (socket);
int rc = zmq_bind (socket, "tcp://*:8080");
assert (rc == 0);
/* Data structure to hold the ZMQ_STREAM routing id */
uint8_t routing_id [256];
size_t routing_id_size = 256;
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
while (1) {
        /*  Get HTTP request; routing id frame and then request */
        routing_id_size = zmq_recv (socket, routing_id, 256, 0);
        assert (routing_id_size > 0);
        do {
                raw_size = zmq_recv (socket, raw, 256, 0);
                assert (raw_size >= 0);
        } while (raw_size == 256);
        /* Prepares the response */
        char http_response [] =
                "HTTP/1\&.0 200 OK\er\en"
                "Content\-Type: text/plain\er\en"
                "\er\en"
                "Hello, World!";
        /* Sends the routing id frame followed by the response */
        zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
        zmq_send (socket, http_response, strlen (http_response), 0);
        /* Closes the connection by sending the routing id frame followed by a zero response */
        zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
        zmq_send (socket, 0, 0, 0);
}
zmq_close (socket);
zmq_ctx_destroy (ctx);
.fi
.if n \{\
.RE
.\}
.sp
.SH "SEE ALSO"
.sp
\fBzmq_init\fR(3) \fBzmq_setsockopt\fR(3) \fBzmq_bind\fR(3) \fBzmq_connect\fR(3) \fBzmq_send\fR(3) \fBzmq_recv\fR(3) \fBzmq_inproc\fR(7) \fBzmq\fR(7)
.SH "AUTHORS"
.sp
This page was written by the 0MQ community\&. To make a change please read the 0MQ Contribution Policy at \m[blue]\fBhttp://www\&.zeromq\&.org/docs:contributing\fR\m[]\&.
